﻿using yao.Common;
using yao.Entity;
using yao.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Titan;

namespace yao.website.Controllers
{
    /// <summary>
    /// 员工管理
    /// </summary>
    public class EmployeeController:BaseController
    {
        public ActionResult Index(int? p)
        {
            if (p == null) {
                p = 1;
            }
            ViewBag.PageIndex = p;
            using (IDbSession cn = OpenSession())
            {
                QueryExpression q = new QueryExpression();
                q.EntityType = typeof(Employee);
                q.Selects.Add(Employee_.EmployeeId);
                q.Selects.Add(Employee_.EmployeeName);
                q.Selects.Add(Employee_.CreateTime);
                q.Selects.Add(Employee_.Mb);
                q.Selects.Add(Employee_.LoginNo);
                q.Selects.Add(Employee_.Email);
                q.Selects.Add(Employee_.IsActive);
                q.Wheres.Add(Employee_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                q.PageIndex = ViewBag.PageIndex;
                q.PageSize = 20;
                q.ReturnMatchedCount = true;
                Employees Employees = new Employees();
                Employees.TotalCount = cn.SelectCollection(Employees.Items, q);
                return View(Employees);
            }
        }

        public ActionResult Edit(long? id)
        {
            Employee Employee = new Employee();
            if (id != null)
            {
                ViewBag.Title = "修改员工-" + id;
                Employee.EmployeeId = (long)id;
                using (IDbSession cn = OpenSession())
                {
                    cn.Select(Employee);
                }
            }
            return View(Employee);
        }

        [HttpPost]
        public ActionResult Edit(long? id, Employee employee)
        {
            #region 验证
            Validate(employee, Employee_.EmployeeName);
            Validate(employee, Employee_.LoginNo);
            #endregion
            if (!IsValid)
            {
                return View(employee);
            }

            using (IDbSession cn = OpenSession())
            {
                employee.CompanyId = CurrentEmployee.CompanyId;
                //添加
                if (id == null)
                {
                    Validate(employee, Employee_.Password);
                    if (!IsValid)
                    {
                        return View(employee);
                    }

                    #region validate
                    ConditionExpressionCollection cs = new ConditionExpressionCollection();
                    cs.Add(Employee_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                    cs.Add(Employee_.LoginNo.TEqual(employee.LoginNo));
                    if (cn.Exists<Employee>(cs))
                    {
                        AddFormError(Employee_.LoginNo, "登录号不允许重复");
                    }
                    if (!IsValid) return View(employee);
                    employee.Password = Encryption.GetMD5HashHex(employee.Password.ToLower());
                    #endregion
                    cn.Insert(employee);
                }
                else
                {
                    employee.EmployeeId = id.Value;
                    #region validate
                    ConditionExpressionCollection cs = new ConditionExpressionCollection();
                    cs.Add(Employee_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                    cs.Add(Employee_.LoginNo.TEqual(employee.LoginNo));
                    cs.Add(Employee_.EmployeeId.TNotEqual(employee.EmployeeId));
                    if (cn.Exists<Employee>(cs))
                    {
                        AddFormError(Employee_.LoginNo, "登录号不允许重复");
                    }
                    if (!IsValid) return View(employee);
                    #endregion

                    cn.Update(employee, Employee_.LoginNo, Employee_.EmployeeName, Employee_.IsActive, Employee_.Mb, Employee_.Email,Employee_.Remark);
                }
            }
            return new IframeResult(Url.Action("Edit"), Url.Action("Index"));
        }

        [HttpPost]
        public ActionResult Delete(long employeeId)
        {
            Employee employee = new Employee { EmployeeId = employeeId };
            bool ret = false;
            //为提高性能，需要时才打开数据库，后面还会有一次数据库验证 
            using (IDbSession cn = OpenSession())
            {
                ret = cn.Delete(employee) > 0;
            }
            return Json(new { ret = ret });
        }

        [HttpPost]
        public ActionResult Active(long employeeId, bool isActive)
        {
            Employee employee = new Employee
            {
                EmployeeId = employeeId,
                IsActive = isActive
            };
            bool ret = false;
            //为提高性能，需要时才打开数据库，后面还会有一次数据库验证 
            using (IDbSession cn = OpenSession())
            {
                ret = cn.Update(employee, Employee_.IsActive) > 0;
            }
            return Json(new { ret = ret });
        }

        [HttpPost]
        public ActionResult ResetPassword(long employeeId)
        {
            Employee employee = new Employee
            {
                EmployeeId = employeeId,
                Password = Encryption.GetMD5HashHex("888888")
            };
            bool ret = false;
            //为提高性能，需要时才打开数据库，后面还会有一次数据库验证 
            using (IDbSession cn = OpenSession())
            {
                ret = cn.Update(employee, Employee_.Password) > 0;
            }
            return Json(new { ret = ret });
        }


        public ActionResult Detail(long id)
        {
            using (IDbSession cn = OpenSession())
            {
                Employee employee = new Employee { EmployeeId = id };
                cn.Select(employee);
                return View(employee);
            }
        }
    }
}
